let App = getApp();
Page({

  data: {
    active: 0,
    category:[],
    scrollTop:0,
    api_url: App.Domain,
    re_value:'',
    queryForm:{
      cate:{},
      page:0,
      loading:false,
      sel_type:"normal",
      noList:false,
      no_more:false
    },
    goodsList:[]
  },

  onLoad: function (options) {
    var that = this;
    //页面启动后 调取首页的数据
    that.setData({
      wxapp: wx.getStorageSync('wxapp')
    });
    App.wx_setcolor(that.data.wxapp);
    //获得分类数据
    App._get('category/Showlist', {}, function (result) {
      that.setData({
        category: result.data.categorydata.map((e,i)=>{return {
          ...e,
          childlist:e.childlist.map(c=>{return {...c,active:false}}),
          active:false
        }})
      })
      // 默认打开第一个类目
      that.panel(undefined,0);
      that.handleChange(undefined,that.data.category[0]['childlist'][0].id)
    });
  },
  // 滚动到下一页
  nextPage(e){
    if(this.data.queryForm.no_more){
      return 
    }else{
      this.getGoodsList()
    }
  },
  // 查询商品列表
  getGoodsList: function (refresh) {
    // 判断当前是否为加载状态
    if(this.data.queryForm.loading){
      return
    }else{
      this.setData({
        queryForm:{...this.data.queryForm,loading:true}
      })
    }
    let that = this;
    let {page,sel_type,cate:{id,name}} = that.data.queryForm;
    App._get('goods/category_list', { 
        id:id || -1, page: refresh ? 1 : page + 1, types: sel_type, 
        name,
    }, function (result) {
      let {listdata,pagedata:{current_page,last_page,total,data}} = result.data;
      let goodsList = that.data.goodsList;
      listdata = listdata.map((e,i)=>{
        return {
          ...e,
          tags:data[i].tags ? data[i].tags.split(',') : '',
          other:data[i].other ||  '',
        }
      })
      that.setData({
        queryForm:{
          ...that.data.queryForm,
          loading:false,
          page:current_page,
          noList:listdata.length == 0,
          no_more: current_page >= last_page
        },
        goodsList:refresh ? listdata : goodsList.concat(listdata),  
      })
    });
  },
  // 分类折叠切换
  panel: function (e,index) {
    index = index != undefined ? index : e.currentTarget.dataset.index;
    let item_height = 62;
    let childlist = this.data.category[index]['childlist'];
    this.data.category[index] = {
      ...this.data.category[index],
      active:e ? !e.currentTarget.dataset.active : true,
      h:!e ? item_height * childlist.length : e.currentTarget.dataset.active ? 0 : item_height * childlist.length
    }
    this.setData({
      category:this.data.category,
      active: index,
    })
  },
  // 子级分类选中
  handleChange(v,id){
    id = id != undefined ? id : v.currentTarget.dataset.id;
    // 防止重复点击
    if(id == this.data.queryForm.cate.id){
      return
    }
    // 正在加载时禁止切换子级分类
    if(this.data.queryForm.loading){
      return
    }
    let queryForm = this.data.queryForm;
    let category = this.data.category;
    category.forEach(e => {
      e.childlist.forEach(c=>{
        if(c.id == id){
          c.active = true;
          queryForm.cate = {...c}
        }else{
          c.active = false;
        }
      })
    });
    this.setData({
      queryForm,
      category
    });
    this.getGoodsList(true)
  },
  onShow: function () {

  },
  onChange: function(event) {
    this.setData({
      active: event.detail
    })
  },
  onSearchchange: function (event) {
    this.setData({
      re_value: event.detail
    })
  },
  // 提交搜索
  onSearch: function (event) {
    // 初始化类目
    this.setData({
      active:0,
      category:this.data.category.map(e=>{
        return {
          ...e,
          active:false,
          h:0,
          childlist:e.childlist.map(c=>{return {...c,active:false}})
        }
      }),
      queryForm:{
        ...this.data.queryForm,
        cate:{
          id:-1,
          name:this.data.re_value
        }
      }
    })
    this.getGoodsList(true);
    // console.log(this.data.re_value);
    //这里跳转到  搜索页
    // wx.navigateTo({
    //   url: './list?rename=1&name=' + this.data.re_value
    // })
  },
  onShareAppMessage: function () {

  },
  onShareTimeline:function(){

  }
})